package com.itcat.api;/*
 * @Author: gonhsl
 * @Date:2022-04-04-13:29
 * */

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itcat.api.mysql.SmsFlashPromotionProductRelationApi;
import com.itcat.mapper.PmsProductMapper;
import com.itcat.mapper.SmsFlashPromotionProductRelationMapper;
import com.itcat.mysql.pojo.PmsProduct;
import com.itcat.mysql.pojo.SmsFlashPromotionProductRelation;
import com.itcat.mysql.vo.SmsFlashPromotionProductRelationVo;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.stream.Collectors;

@DubboService
public class SmsFlashPromotionProductRelationApiImpl implements SmsFlashPromotionProductRelationApi {
    @Autowired
    private SmsFlashPromotionProductRelationMapper smsFlashPromotionProductRelationMapper;
    @Autowired
    private PmsProductMapper pmsProductMapper;
    @Override
    public List<SmsFlashPromotionProductRelationVo> list(Integer pageNum, Integer pageSize, Integer flashPromotionId, Integer flashPromotionSessionId) {
        LambdaQueryWrapper<SmsFlashPromotionProductRelation> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SmsFlashPromotionProductRelation::getFlashPromotionId,flashPromotionId)
                .eq(SmsFlashPromotionProductRelation::getFlashPromotionSessionId,flashPromotionSessionId);
        List<SmsFlashPromotionProductRelation> smsFlashPromotionProductRelations = smsFlashPromotionProductRelationMapper.selectList(wrapper);

        List<SmsFlashPromotionProductRelationVo> vos = smsFlashPromotionProductRelations.stream().map(item -> {
            PmsProduct pmsProduct = pmsProductMapper.selectById(item.getProductId());
            SmsFlashPromotionProductRelationVo smsFlashPromotionProductRelationVo = new SmsFlashPromotionProductRelationVo();
            BeanUtils.copyProperties(item,smsFlashPromotionProductRelationVo);
            smsFlashPromotionProductRelationVo.setProduct(pmsProduct);
            return smsFlashPromotionProductRelationVo;
        }).collect(Collectors.toList());

        return vos;
    }

    @Override
    public void create(List<SmsFlashPromotionProductRelation> relationList) {
        for (SmsFlashPromotionProductRelation smsFlashPromotionProductRelation : relationList) {

            smsFlashPromotionProductRelationMapper.insert(smsFlashPromotionProductRelation);
        }
    }

    @Override
    public void delete(Integer id) {
        smsFlashPromotionProductRelationMapper.deleteById(id);
    }

    @Override
    public void update(Integer id, SmsFlashPromotionProductRelation relation) {
     smsFlashPromotionProductRelationMapper.updateById(relation);
    }

    @Override
    public SmsFlashPromotionProductRelation findOne(Integer id) {
        SmsFlashPromotionProductRelation smsFlashPromotionProductRelation = smsFlashPromotionProductRelationMapper.selectById(id);
        return smsFlashPromotionProductRelation;
    }
}
